/**
 * FormFieldSwitch
 */
layui.define(['jquery','FormField', 'form'], function (exports) {

    let doc = document;
    let $ = layui.jquery;
    let FormField = layui.FormField;
    let form = layui.form;

    class FormFieldSwitch extends FormField {

        constructor(options = {}) {
            super(options);
            this.onVal = '';
            this.offVal = '';
        }

        defineExtraArgs() {
            let that = this;
            return {
                options :[],
            };
        }

        handleArgs(args) {
            let options = args.options;
            if (!yunj.isArray(options)) {
                throw new Error('类型[switch]配置[options]错误');
            }
            if (options.length <= 0) {
                options = [
                    { label: '开', value: "on" },
                    { label: '关', value: "off" },
                ];
            }
            if (args.verify.indexOf('in') === -1) {
                let values = [];
                options.forEach(option=>{
                    values.push(option.value);
                });
                args.verify += (args.verify ? "|" : "") + `in:${values.join(",")}`;
            }
            args.options = options;
            return args;
        }

        layoutControl() {
            let that = this;
            let texts = [];
            that.args.options.forEach(option=>{
                texts.push(option.label);
            });
            let controlHtml = `<input type="checkbox" name="${that.id}" lay-text="${texts.join("|")}" } lay-skin="switch" lay-filter="${that.id}" ${that.args.readonly ? 'disabled' : ''}>`;
            return `<div class="layui-input-inline yunj-form-item-control yunj-input-pane">${controlHtml}</div>`;
        }

        async renderBefore() {
            let that = this;
            let values = [];
            that.args.options.forEach(option=>{
                values.push(option.value);
            });
            [that.onVal, that.offVal] = values;
            return 'done';
        }

        async setValue(val = '') {
            let that = this;
            val = val || "";
            that.boxEl.find(`input:checkbox[name=${that.id}]`).prop('checked', val.toString() === that.onVal.toString());
            form.render('checkbox', that.tabFormFilter);
        }

        getValue() {
            let that = this;
            return that.isChecked() ? that.onVal : that.offVal;
        }

        renderDone() {
            let that = this;
            form.render('checkbox', that.tabFormFilter);
        }

        defineExtraEventBind() {
            let that = this;
            // 监听点击事件
            form.on(`switch(${that.id})`, function(data){
                // 触发点击事件
                that.outermostEl.trigger(`yunj_form_${that.id}_switch`, [that]);
            });
        }

        /**
         * 当前是否选中
         * @return {boolean}
         */
        isChecked() {
            let that = this;
            return that.boxEl.find(`input:checkbox[name=${that.id}]`).is(':checked');
        }

    }

    exports('FormFieldSwitch', FormFieldSwitch);
});